package clientapp;

import org.xml.sax.*;
import org.xml.sax.helpers.*;


public class xmlHandler extends DefaultHandler {
  private ControllerMIDlet midlet;
  public SAXAfdeling afdeling;
  public SAXHHS hhs;
  public SAXOpleiding opleiding;
  

    private boolean readingInfo;
    private boolean readingNaam;
    private boolean readingLocatie;

  public xmlHandler (ControllerMIDlet midlet)
  {
    this.midlet = midlet;
  }

  public void startDocument() throws SAXException {}

  public void startElement(String uri, String localName, String qName,
    Attributes attributes) throws SAXException
  {
    if(qName.equalsIgnoreCase("HHS"))
    {
      hhs = new SAXHHS();
      
    }
        else if (qName.equalsIgnoreCase("afdeling")) {
            afdeling = new SAXAfdeling(attributes.getValue(0));
            hhs.addAfdeling(afdeling);         
        }
        else if (qName.equalsIgnoreCase("opleiding")) {
           opleiding = new SAXOpleiding();
           afdeling.addOpleiding(opleiding);
        }
        else if (qName.equalsIgnoreCase("info")) {
            
            this.readingInfo = true;
        }
        else if (qName.equalsIgnoreCase("naam")) {
            this.readingNaam = true;
        }
        else if (qName.equalsIgnoreCase("locatie")) {
            this.readingLocatie = true;
        }
        else {
            System.out.println("Unknown element: " + qName);
        }
  
      
  }

  public void characters(char[] ch, int start, int length) throws SAXException
  {
    String chars = new String(ch, start, length).trim();
        if (chars.length() == 0) {
            
            return;
        }
        opleiding = afdeling.getLastOpleiding();
        if (readingInfo) {
            
            opleiding.setInfo(chars);
        }
        else if (readingNaam) {
            opleiding.setNaam(chars);
        }
        else if (readingLocatie) {
            opleiding.setLocatie(chars);
            
        }
    }
  

  public void endElement(String uri, String localName, String qName)
  { try {
              if (qName.equalsIgnoreCase("info")) {
            this.readingInfo = false;
                  
        }
        else if (qName.equalsIgnoreCase("naam")) {
           
            this.readingNaam = false;
        }
        else if (qName.equalsIgnoreCase("locatie")) {
             
            this.readingLocatie = false;
        }
   
    } catch (Exception e) {
              
              System.out.println(e);
    }
  }

    public void endDocument() throws SAXException {}
}

